<?php
	session_start();
	
	require_once('../../lib/'.'BaseDir.inc.htm');
	require_once (EIU_LIB.'View.class.htm');
	require_once('../../lib/'.'DBCommon.class.htm');
	
	$today = date("Y-m-d h:i:s");
	$modifiedby = "";
	if(isset($_SESSION['userName']) && $_SESSION['userName']!=""){
		$modifiedby = $_SESSION['userName'];
	}
	
	if(isset($_POST['isDeleted']) && $_POST['isDeleted']==1){
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		//update isDeleted
		$q_del_customer = "UPDATE `cus_list_tbl` SET `isDeleted` = '1',
								`LastUpdate` = '".$today."',
								`ModifiedBy` = '".$modifiedby."'
								WHERE `cus_list_tbl`.`CusID` = '".$_POST['cusId']."'";
								
		$rs_delete = $oDB->stl_db_change($q_del_customer, $conn);
		echo $rs_delete;
		
		$oDB->stl_closeConn($conn);
	}
	
	if(isset($_POST['info']) && !empty($_POST['info'])){
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		
		$customer = json_decode($_POST['info'],true);
		if(is_array($customer)){
			//insert an employee
			if(empty($_POST['cusId'])){
				$q_insert_customer = "INSERT INTO `cus_list_tbl` (
					`ComName` ,`ShortName`,`ComAddress` ,
					`ComTel` ,	`ComFax` ,`BankName` ,
					`BankCN` ,`BankAcc` ,`ParentID`  ,
					`isDeleted`,`CreateDate`,`ModifiedBy`
					)
					VALUES (
					'".$customer['name']."','".$customer['shortname']."', '".$customer['address']."', 
					'".$customer['tel']."', '".$customer['fax']."', '".$customer['bankname']."', 
					'".$customer['bankcn']."', '".$customer['bankacc']."','".$customer['parentid']."', 
					'0', '".$today."', '".$modifiedby."'
					);";
				
				$rs_insert = $oDB->stl_db_change($q_insert_customer, $conn);
				echo $rs_insert;
			}else{
				//update cus_list_tbl
				$q_update_customer = "UPDATE `cus_list_tbl` SET `ComName` = '".$customer['name']."',
					`ShortName` = '".$customer['shortname']."',
					`ComAddress` = '".$customer['address']."',
					`ComTel` = '".$customer['tel']."',
					`ComFax` = '".$customer['fax']."',					
					`BankName` = '".$customer['bankname']."',
					`BankCN` = '".$customer['bankcn']."',
					`BankAcc` = '".$customer['bankacc']."',
					`ParentID` = '".$customer['parentid']."',					
					`ModifiedBy` = '".$modifiedby."',
					`LastUpdate` = '".$today."'
					 WHERE `cus_list_tbl`.`CusID` = '".$_POST['cusId']."'";
				//	print_r($q_update_customer);
				$rs_update = $oDB->stl_db_change($q_update_customer, $conn);
				echo $rs_update;
			}
		}
		
		$oDB->stl_closeConn($conn);
	}
?>
